package com.controller;

import com.dto.ActivityApplyDetailDTO;
import com.dto.ActivityDTO;
import com.dto.BookDTO;
import com.dto.DonationDTO;
import com.service.ActivityServiceImpl;
import com.service.LoginServiceImpl;
import com.util.LykFileUtil;
import com.util.Result;
import com.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author linyukun
 * @date 2022/10/8 15:57
 */
@RequestMapping("activity")
@RestController
public class ActivityController {
    @Autowired
    private ActivityServiceImpl activityServiceImpl;
    @Autowired
    private LoginServiceImpl loginServiceImpl;

    @Value("${image.upload.path.activity}")
    private String imageUploadPathActivity;

    //审核中的数据
    private volatile Map<String,String> activityControllerMap = new HashMap<>();
    private static final String activityControllerMapSeparator = "*****";

    /**
     * 添加活动
     * @author linyukun
     * @date 2022-10-08 16:00
     */
    @RequestMapping(value="/addActivity",method = RequestMethod.POST)
    public Result addActivity(@RequestBody ActivityDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setCreateUser(userName);
            return ResultUtil.returnDataSuccess(activityServiceImpl.addActivity(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取活动列表
     * @author linyukun
     * @date 2022-10-08 16:00
     */
    @RequestMapping(value="/getActivityList",method = RequestMethod.POST)
    public Result getActivityList(@RequestBody ActivityDTO dto){
        try{
            return ResultUtil.returnDataSuccess(activityServiceImpl.getActivityList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取活动列表（用户）
     * @author linyukun
     * @date 2022-10-08 16:00
     */
    @RequestMapping(value="/getActivityUserList",method = RequestMethod.POST)
    public Result getActivityUserList(@RequestBody ActivityDTO dto){
        try{
            dto.setIsUser("1");
            String userName = loginServiceImpl.isLogin();
            dto.setCreateUser(userName);
            return ResultUtil.returnDataSuccess(activityServiceImpl.getActivityList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 删除赠书信息
     * @author linyukun
     * @date 2022-10-08 11:00:00
     */
    @RequestMapping(value="/delActivity",method = RequestMethod.POST)
    public Result delActivity(@RequestBody ActivityDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setDeleteUser(userName);
            return ResultUtil.returnDataSuccess(activityServiceImpl.delActivity(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 修改赠书信息
     * @author linyukun
     * @date 2022-10-08 11:00:00
     */
    @RequestMapping(value="/updateActivity",method = RequestMethod.POST)
    public Result updateActivity(@RequestBody ActivityDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setUpdateUser(userName);
            return ResultUtil.returnDataSuccess(activityServiceImpl.updateActivity(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 修改活动信息
     * @author linyukun
     * @date 2022-10-08 11:00:00
     */
    @RequestMapping(value="/getActivityById",method = RequestMethod.POST)
    public Result getActivityById(@RequestBody ActivityDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess(activityServiceImpl.getActivityById(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * 发布活动信息
     * @author linyukun
     * @date 2022-10-08 18:00:00
     */
    @RequestMapping(value="/publishActivity",method = RequestMethod.POST)
    public Result publishActivity(@RequestBody ActivityDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            return ResultUtil.returnDataSuccess(activityServiceImpl.publishActivity(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    //保存活动海报
    @RequestMapping("/saveActivityPoster")
    public String saveActivityPoster(MultipartFile file) throws IOException {
        MultipartFile[] files = new MultipartFile[]{file};
        //保存图片文件到本地并返回相对路径
        //List<String> image = LykFileUtil.uploads("/planCat_file/", files, "image");
        String currentPath = System.getProperty("user.dir");
        List<String> image = LykFileUtil.uploads(imageUploadPathActivity, files, "image");
        //拿着图片路径存储到数据库或者做其他处理,这里就先不多写了，自行发挥
        if(!CollectionUtils.isEmpty(image)){
            //return image.get(0).replace("/view/src/main/resources/static/","");
            return currentPath + image.get(0);
        }else{
            return "";
        }
    }

    //报名活动
    @RequestMapping("/applyActivity")
    public Result applyActivity(@RequestBody ActivityApplyDetailDTO dto) throws IOException {
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setUserName(userName);
            return ResultUtil.returnDataSuccess(activityServiceImpl.applyActivity(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    //获取报名列表
    @RequestMapping("/getActivityApplyDetailList")
    public Result getActivityApplyDetailList(@RequestBody ActivityApplyDetailDTO dto) throws IOException {
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setUserName(userName);
            return ResultUtil.returnDataSuccess(activityServiceImpl.getActivityApplyDetailList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    //获取报名列表
    @RequestMapping("/delActivityApplyDetailById")
    public Result delActivityApplyDetailById(@RequestBody ActivityApplyDetailDTO dto) throws IOException {
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setDeleteUser(userName);
            return ResultUtil.returnDataSuccess(activityServiceImpl.delActivityApplyDetailById(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


}
